/**
 * 
 */
package com.gmail.blackdog1987.onedollar.web.controller;

import com.gmail.blackdog1987.onedollar.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @author 董国超
 *
 */
@Controller
public class OrderController extends BaseController {
    @Autowired
    OrderService orderService;

    Lock lock = new ReentrantLock();// 锁
    /**
     * 获取订单列表
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getOrderList", method=RequestMethod.POST)
    public Map<String, Object> getOrderList(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.get(filter);
        return decorateResult(result);
    }

    /**
     * 下单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="submitOrder", method=RequestMethod.POST)
    public Map<String, Object> submitOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.createOrder(filter);
        return decorateResult(result);
    }

    /**
     * 1元十元下单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="submitOneDollarOrder", method=RequestMethod.POST)
    public Map<String, Object> submitOneDollarOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        lock.lock();
            Map<String, Object> result = orderService.createOneDollarOrder(filter);
        lock.unlock();// 释放锁

        return decorateResult(result);
    }

    /**
     * 兑换
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="exchangeOneDollar", method=RequestMethod.POST)
    public Map<String, Object> exchangeOneDollar(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.createExchangeOneDollarOrder(filter);
        return decorateResult(result);
    }

    /**
     * 删除订单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="delOrder", method=RequestMethod.POST)
    public Map<String, Object> delOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.deleteOrder(filter);
        return decorateResult(result);
    }

    /**
     * 支付订单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="payOrder", method=RequestMethod.POST)
    public Map<String, Object> payOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.doPay(filter);
        return decorateResult(result);
    }

    /**
     * 支付订单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="payOneDollarOrder", method=RequestMethod.POST)
    public Map<String, Object> payOneDollarOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.doPayOneDollar(filter);
        return decorateResult(result);
    }

    /**
     * 申请退货
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="refoundOrder", method=RequestMethod.POST)
    public Map<String, Object> refoundOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.doRefound(filter);
        return decorateResult(result);
    }

    /**
     * 评价订单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="judgeOrder", method=RequestMethod.POST)
    public Map<String, Object> judgeOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.doJudge(filter);
        return decorateResult(result);
    }

    /**
     * 撤销订单
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="cancelOrder", method=RequestMethod.POST)
    public Map<String, Object> cancelOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.doCancel(filter);
        return decorateResult(result);
    }

    /**
     * 确认收获
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="receiveOrder", method=RequestMethod.POST)
    public Map<String, Object> receiveOrder(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = orderService.updateForReceiveOrder(filter);
        return decorateResult(result);
    }

    @Override
    protected Map<String, Object> decorateResult(Map<String, Object> result) {
        result.put("time", System.currentTimeMillis());
        return result;
    }
}
